﻿using System;
using System.Collections.Generic;
using System.Text;
using Atosenet.PluginEngine;

namespace Atosenet.Protocol
{
    [PluginAttribute("Atosenet", "Atosenet.Protocol.ProtocolPacketATPBEx", "ATOS工业标准扩展协议", PluginType.Protocol)]
    public class ProtocolPacketATPBEx : ProtocolPacketATPB
    {
        #region 公开函数

        public override object Clone()
        {
            ProtocolPacketATPBEx packet = ProtocolPacketATPBEx.CreatePacket(this.CMD, this.DATA);
            packet._receiveTime = packet.Timestamp;
            return packet;
        }

        public override string ToString()
        {
            string result = string.Format("ATP-B-EX Protocol Packet{0}([SOP]={1},[CMD]={2},[LEN]={3},[HCS]={4},[DATA]={5},[PCS]={6})",
                Environment.NewLine,
                Converter.ConvertToHexString(new byte[] { ASP_SOP_1, ASP_SOP_2 }),
                CMD.ToString("X2"),
                LEN.ToString("X2"),
                HCS.ToString("X2"),
                Converter.ConvertToHexString(DATA),
                PCS.ToString("X2"));
            return result;
        }

        #endregion


        #region 静态函数

        public new static ProtocolPacketATPBEx CreatePacket(byte fieldCmd, byte[] fieldData)
        {
            ProtocolPacketATPBEx packet = new ProtocolPacketATPBEx();
            packet.CMD = fieldCmd;
            packet.DATA = fieldData;
            byte[] data = packet.Generate();
            return ProtocolPacketATPBEx.CreatePacket(data);
        }

        public new static ProtocolPacketATPBEx CreatePacket(byte fieldCmd)
        {
            ProtocolPacketATPBEx packet = new ProtocolPacketATPBEx();
            packet.CMD = fieldCmd;
            packet.DATA = new byte[] { };
            packet.Generate();
            byte[] data = packet.Generate();
            return ProtocolPacketATPBEx.CreatePacket(data);
        }

        public new static ProtocolPacketATPBEx CreatePacket(byte[] rawData)
        {
            ProtocolPacketATPBEx packet = new ProtocolPacketATPBEx();
            int offset = 0;
            packet.Append(rawData, ref offset);
            return packet;
        }

        public new static ProtocolPacketATPBEx CreatePacket()
        {
            ProtocolPacketATPBEx packet = new ProtocolPacketATPBEx();
            return packet;
        }

        #endregion
    }
}
